#!/bin/bash

# 初始化环境变量
app_service_dockerfile_path=./release/dockerfiles/Dockerfile-node24
app_service_image="${app_service_image}"


build_service_image(){
  echo "复制Dockerfile"
  echo "cp ${app_service_dockerfile_path} ./Dockerfile"--tag
  cp ${app_service_dockerfile_path} ./Dockerfile

  echo "编译打包"
  echo "pnpm install"
  gradle pnpm install
  echo "pnpm build"
  gradle pnpm build

  echo "清理旧镜像"
  echo "docker rmi -f ${app_service_image} || true"
  docker rmi -f ${app_service_image} || true

  echo "构建镜像"
  echo "docker build --build-arg APP_DIR=.output/ --tag=${app_service_image} ."
  docker build --build-arg APP_DIR=.output/ --tag=${app_service_image} .
}

push_service_image(){
  echo "推送镜像"
  echo "docker push ${app_service_image}"
  docker push ${app_service_image}

  echo "清理镜像"
  echo "docker image rm ${app_service_image}"
  docker image rm ${app_service_image}
}

clean_service_image(){
  echo "清理镜像"
  echo "docker image rm ${app_service_image}"
  docker image rm ${app_service_image}
}

# 主逻辑
case $1 in
  build)
    build_service_image
    ;;
  push)
    push_service_image
    ;;
  clean)
    clean_service_image
    ;;
  *)
    echo "Usage: $0 {check|run|rerun|clean}"
    exit 1
    ;;
esac